package plugins.shard_plugin;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.geom.Rectangle2D;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import dymnd.Layer;
import dymnd.net.Packet;
import dymnd.plugin.PaintPlugin;
import dymnd.ui.SeekBar;
import dymnd.ui.ThemeConstants;
public class EPC extends PaintPlugin{
	
	SeekBar sizeSeekbar = new SeekBar(200);
	SeekBar opacitySeekbar = new SeekBar(100);
	
	public char getKeyChar()	{
		return 's';
	}
	
	public Container getToolbar(Dimension d)	{
		Container c = new Container();
		c.setLocation(0, 0);
		c.setSize(d.getSize());
		int labelWidth = 50;
		int componentHeight = 20;
		
		int sizeSeekbarWidth = 150;
		int opacitySeekbarWidth = 100;
		
		int x = 10;
		
		JLabel sizeLabel = new JLabel("Size: ");
		JLabel opacityLabel = new JLabel("Opacity: ");
		
		//size seekbar
		sizeLabel.setSize(30, componentHeight);
		sizeLabel.setLocation(x, (int)(d.getHeight()/2 - sizeLabel.getHeight()/2));
		sizeLabel.setForeground(ThemeConstants.THEME_PLUGIN_PANEL_TEXT_COLOR);
		sizeLabel.setFont(ThemeConstants.FONT_PLUGIN_TOOLBAR_COMPONENT);
		c.add(sizeLabel);
		
		sizeSeekbar.setSize(sizeSeekbarWidth, componentHeight);
		sizeSeekbar.setLocation(x += sizeLabel.getWidth(),(int)(d.getHeight()/2 - sizeSeekbar.getHeight()/2));
		sizeSeekbar.setPercent(10);
		c.add(sizeSeekbar);
		
		//opacity seekbar 
		opacityLabel.setSize(labelWidth, componentHeight);
		opacityLabel.setLocation(x += sizeSeekbar.getWidth() + 10, (int)(d.getHeight()/2 - sizeLabel.getHeight()/2));
		opacityLabel.setForeground(ThemeConstants.THEME_PLUGIN_PANEL_TEXT_COLOR);
		opacityLabel.setFont(ThemeConstants.FONT_PLUGIN_TOOLBAR_COMPONENT);
		c.add(opacityLabel);
		
		opacitySeekbar.setSize(opacitySeekbarWidth, componentHeight);
		opacitySeekbar.setLocation(x += opacityLabel.getWidth(),(int)(d.getHeight()/2 - opacitySeekbar.getHeight()/2));
		c.add(opacitySeekbar);
		
		
		return c;
	}
	
	public String getVersionString()	{
		return "shard_plugin.v1.0";
	}
	
	public Rectangle2D onEvent(Packet p, Layer layer, Layer scratchLayer)	{
		
		int radius = (int)sizeSeekbar.getValue();
		
		int x = Math.min(p.x, p.lastX) - radius;
		int y = Math.min(p.y, p.lastY) - radius;
		
		if(x < 0)
			x = 0;
		if(y < 0)
			y = 0;
		
		if(x > layer.getWidth())
			x = layer.getWidth() - radius*2+1;
		if(y > layer.getHeight())
			y = layer.getWidth() - radius*2+1;
		
		int width = (Math.max(p.x, p.lastX) - Math.min(p.x, p.lastX)) + radius*2;
		int height = (Math.max(p.y, p.lastY) - Math.min(p.y, p.lastY)) + radius*2;
		
		
		if(x + width > layer.getWidth())	{
			width = width + (layer.getWidth() - (x+width));
		}
		
		if(y + height > layer.getHeight())	{
			height = height + (layer.getHeight() - (y+height));
		}
		
		if(width <= 0)	{
			x = layer.getWidth() - 2;
			width = 1;
		}
		
		if(height <= 0)	{
			y = layer.getHeight() - 2;
			height = 1;
		}
		
		
		
		
		
		
		
		
		return new Rectangle2D.Double(x, y, width, height);
	}
	
	public ImageIcon getIcon()	{
		return new ImageIcon(Toolkit.getDefaultToolkit().getImage("plugins/shard_plugin/shardicon.png"));
	}
	
	public String getTooltip()	{
		return "Creates a shard-like stroke";
	}
	
	public boolean usingDefaultCursor()	{
		return true;
	}
	
	
	 public void onUnload()	{
		 
	 }
	
	

}
